package cn.bonoon.kernel.web.controllers;

import javax.servlet.http.HttpServletRequest;

import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.menus.ItemInfo;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.security.PrivilegeChecker;
import cn.bonoon.kernel.support.services.UserService;
import cn.bonoon.kernel.web.StandardKeyChecker;
import cn.bonoon.kernel.web.models.FrameModel;
import cn.bonoon.kernel.web.models.PanelModel;

public class StandardPanelParser implements PanelParser {

	protected final ModuleManager moduleManager;
	protected final UserService userService;
	
	public StandardPanelParser(ModuleManager moduleManager, UserService userService) {
		this.moduleManager = moduleManager;
		this.userService = userService;
	}

	@Override
	public PanelEvent parse(String mid, HttpServletRequest request, LogonUser user) throws Exception{
		FrameModel model = new FrameModel(mid, request);
		ItemInfo item = moduleManager.find(model);
		return accessCheck(model, item, user);
	}
	
	/**
	 * <pre>
	 * 子类需要指定界面的框架，可定义为：
	 * 1.layout界面框架，可以使用{@link cn.bonoon.kernel.web.models.LayoutModel LayoutModel}
	 * 2.frame界面框架，可以使用{@link cn.bonoon.kernel.web.models.FrameModel FrameModel}
	 * </pre>
	 * @param mid
	 * @param request
	 * @return
	 */
	protected PanelEvent accessCheck(PanelModel model, ItemInfo item, LogonUser user) throws Exception{
		
		StandardKeyChecker.setChecker(model, item, user, userService);
		
		PrivilegeChecker privilege = userService.privilege(user, item);
		
		return new PanelEvent(model, item, user, privilege);
	}
}
